<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="老人编号" prop="oldId">
      <el-input v-model="dataForm.oldId" placeholder="老人编号"></el-input>
    </el-form-item>
    <el-form-item label="老人名字" prop="oldName">
      <el-input v-model="dataForm.oldName" placeholder="老人名字"></el-input>
    </el-form-item>
    <el-form-item label="楼区" prop="building">
      <el-input v-model="dataForm.building" placeholder="楼区"></el-input>
    </el-form-item>
    <el-form-item label="房间号" prop="roomId">
      <el-input v-model="dataForm.roomId" placeholder="房间号"></el-input>
    </el-form-item>
    <el-form-item label="床号" prop="bedId">
      <el-input v-model="dataForm.bedId" placeholder="床号"></el-input>
    </el-form-item>
    <el-form-item label="转换楼区" prop="changeBuilding">
      <el-input v-model="dataForm.changeBuilding" placeholder="转换楼区"></el-input>
    </el-form-item>
    <el-form-item label="转换房间号" prop="changeRoom">
      <el-input v-model="dataForm.changeRoom" placeholder="转换房间号"></el-input>
    </el-form-item>
    <el-form-item label="转换床号" prop="changeBed">
      <el-input v-model="dataForm.changeBed" placeholder="转换床号"></el-input>
    </el-form-item>
    <!-- <el-form-item label="正式入住日期" prop="checkinDate">
      <el-input v-model="dataForm.checkinDate" placeholder="正式入住日期"></el-input>
    </el-form-item>
    <el-form-item label="换房日期" prop="dateOfChange">
      <el-input v-model="dataForm.dateOfChange" placeholder="换房日期"></el-input>
    </el-form-item> -->
    <el-form-item label="正式入住日期" prop="checkinDate">
      <el-date-picker
      v-model="dataForm.checkinDate"
      type="datetime"
      placeholder="选择日期"
      format="yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒"
      value-format="yyyy-MM-dd HH:mm:ss">
    </el-date-picker>
    </el-form-item>
    <el-form-item label="换房日期" prop="dateOfChange">
      <el-date-picker
      v-model="dataForm.dateOfChange"
      type="datetime"
      placeholder="选择日期"
      format="yyyy 年 MM 月 dd 日 HH 时 mm 分 ss 秒"
      value-format="yyyy-MM-dd HH:mm:ss">
    </el-date-picker>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          oldId: '',
          oldName: '',
          building: '',
          roomId: '',
          bedId: '',
          changeBuilding: '',
          changeRoom: '',
          changeBed: '',
          checkinDate: '',
          dateOfChange: ''
        },
        dataRule: {
          oldId: [
            { required: true, message: '老人编号不能为空', trigger: 'blur' }
          ],
          oldName: [
            { required: true, message: '老人名字不能为空', trigger: 'blur' }
          ],
          building: [
            { required: true, message: '楼区不能为空', trigger: 'blur' }
          ],
          roomId: [
            { required: true, message: '房间号不能为空', trigger: 'blur' }
          ],
          bedId: [
            { required: true, message: '床号不能为空', trigger: 'blur' }
          ],
          changeBuilding: [
            { required: true, message: '转换楼区不能为空', trigger: 'blur' }
          ],
          changeRoom: [
            { required: true, message: '转换房间号不能为空', trigger: 'blur' }
          ],
          changeBed: [
            { required: true, message: '转换床号不能为空', trigger: 'blur' }
          ],
          checkinDate: [
            { required: true, message: '正式入住日期不能为空', trigger: 'blur' }
          ],
          dateOfChange: [
            { required: true, message: '换房日期不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/wgztransferregistration/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.oldId = data.wgzTransferRegistration.oldId
                this.dataForm.oldName = data.wgzTransferRegistration.oldName
                this.dataForm.building = data.wgzTransferRegistration.building
                this.dataForm.roomId = data.wgzTransferRegistration.roomId
                this.dataForm.bedId = data.wgzTransferRegistration.bedId
                this.dataForm.changeBuilding = data.wgzTransferRegistration.changeBuilding
                this.dataForm.changeRoom = data.wgzTransferRegistration.changeRoom
                this.dataForm.changeBed = data.wgzTransferRegistration.changeBed
                this.dataForm.checkinDate = data.wgzTransferRegistration.checkinDate
                this.dataForm.dateOfChange = data.wgzTransferRegistration.dateOfChange
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/wgztransferregistration/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'oldId': this.dataForm.oldId,
                'oldName': this.dataForm.oldName,
                'building': this.dataForm.building,
                'roomId': this.dataForm.roomId,
                'bedId': this.dataForm.bedId,
                'changeBuilding': this.dataForm.changeBuilding,
                'changeRoom': this.dataForm.changeRoom,
                'changeBed': this.dataForm.changeBed,
                'checkinDate': this.dataForm.checkinDate,
                'dateOfChange': this.dataForm.dateOfChange
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
